package com.zivwu.library.ui.photo;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.zivwu.library.PhotoPicker;
import com.zivwu.library.R;
import com.zivwu.library.dao.bean.Photo;
import com.zivwu.library.ui.base.IActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class PhotoPreviewActivity extends IActivity {
    @Bind(R.id.vp_photo)
    ViewPager vpPhoto;

    @Bind(R.id.tv_indicator)
    TextView tv_indicator;

    private int current_page_index = 0;
    private List<Photo> photoList;
    private RequestOptions requestOptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initPrams(getIntent().getExtras());
        setContentView(R.layout.ziv_activity_photo_preview);
        ButterKnife.bind(this);
        initViewPager();
    }

    protected void initPrams(Bundle extras) {
        if (extras == null)
            return;
        current_page_index = extras.getInt(PhotoPicker.EXTRA_PHOTO_INDEX, current_page_index);
        photoList = extras.getParcelableArrayList(PhotoPicker.EXTRA_PREVIEW_PHOTO);
    }


    private void initViewPager() {
        if (photoList == null) {
            showToast(R.string.ziv_load_fail);
            finish();
        }

        List<ImageView> views = new ArrayList<>();
        for (Photo photo : photoList) {
            views.add(getItem(photo));
        }

        vpPhoto.setAdapter(new ImageAdapter(views));
        vpPhoto.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                CharSequence text = getString(R.string.ziv_viewpager_indicator, position + 1, vpPhoto.getAdapter().getCount());
                setIndicator(text);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        CharSequence text = getString(R.string.ziv_viewpager_indicator, current_page_index + 1, vpPhoto.getAdapter().getCount());
        setIndicator(text);
        vpPhoto.setCurrentItem(current_page_index);
    }

    private void setIndicator(CharSequence text) {
        tv_indicator.setText(text);
    }

    private ImageView getItem(Photo photo) {
        ImageView view = (ImageView) LayoutInflater.from(this)
                .inflate(R.layout.ziv_item_photo_preview, vpPhoto, false);
        Glide.with(vpPhoto.getContext())
                .asBitmap()
                .load(photo.getData())
                .apply(getImageRequestOptions())
                .thumbnail(0.1f)
                .into(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        return view;
    }


    private RequestOptions getImageRequestOptions() {
        if (requestOptions != null)
            return requestOptions;
        requestOptions = new RequestOptions();
        requestOptions
                .fitCenter();
        return requestOptions;
    }

    private class ImageAdapter extends PagerAdapter {
        private List<ImageView> photos;

        private ImageAdapter(List<ImageView> photos) {
            this.photos = photos;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(photos.get(position));
            return photos.get(position);
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(photos.get(position));
        }

        @Override
        public int getCount() {
            return photoList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}
